#!/usr/local/bin/python2.7

import writer, os, string
from constants import *

class RstWriter(writer.Writer):
    """
    Used to write a plant component xml element to the specified directory
    path.  The filename is generated automatically from the Element
    tags/field data.
    """

    def __init__(self):
        writer.Writer.__init__(self)
        self._extension = 'rst'

        self._rst = ''
        self._ind_level = 0
        self._curelem = None

        self._picpath = Constants.pics_dir

    def _writeIt(self):
        self._reset()
        self._buildHeader()
        self._buildRst()
        self._buildLinks()
        self._buildPics()
        
        with open(self._dst, 'w') as myfile:
            myfile.write(self._rst)

    def _reset(self):
        self._rst = ''

    def _buildHeader(self):
        uniqid = ''
        name = ''

        uniqid = self._component.getValue('gen', 'UniqueIDNumber')
        name = self._component.getValue('gen', 'ItemName')

        if len(name) == 0: name = 'No-Name'
        else: name = name[0]
        if len(uniqid) == 0: uniqid = 'No-ID'
        else: uniqid = uniqid[0]

        title = uniqid + ' (' + name + ')'
        und = '~' * len(title)
        self._rst += title + '\n' + und

    def _ind(self):
        ind_space = ' ' * 4
        return ind_space * self._ind_level

    def _buildRst(self):

        prop_dicts = ['gen', 'mech', 'elec', 'iandc']
        name_map = {'mech':'Mechanical', 'elec':'Electrical', \
                    'iandc':'I and C'}

        for name in prop_dicts:
            self._ind_level = 0
            if name != 'gen':
                self._rst += '\n\n'
                self._rst += self._ind() + ':' + name_map[name] + ':'
                self._ind_level = 1

            curr_dict = self._component.getDict(name)
            temp_level = self._ind_level
            for field, vals in curr_dict.items():
                self._rst += '\n\n'

                self._rst += self._ind() + ':' + field[:15] + ': '
                if len(vals) > 1:
                    self._rst += '\n\n'
                    self._ind_level += 1
                else:
                    self._ind_level = 0
                    
                for val in vals:
                    self._rst += self._ind() + val + '\n\n'

                self._rst = self._rst.strip()
                self._ind_level = temp_level

    def _buildPics(self):
        self._rst += '\n\n'
        header = 'Pictures'
        underline = '+' * len(header)
        self._rst += header + '\n' + underline + '\n\n'

        pics = self._component.getPics()

        for pic in pics:
            name = pic.name
            path = pic.path

            picpath = os.path.join(path, name)
            picpath = picpath.replace('\\', '/')
            picpath = picpath.replace(' ', '%20')

            self._rst += '.. image:: file:///' + picpath + '\n'
            self._rst += '    :scale: 100 %\n'
            self._rst += '    :width: 500 px\n\n'

    def _buildLinks(self):
        self._rst += '\n\n'
        to_root = self._component.path_to_root
        from_root = self._component.path_from_root
        path = os.path.join(to_root, from_root)
        path = path.replace('\\', '/')
        path = path.replace(' ', '%20')

        self._rst += ':Original file: <file:///' + path + '>' + '\n\n'

